import wx

class EditorParent(object):
	"""
	Interface implemented by MainWindow that allows Editors to communicate with it.
	"""
	
	def closeEditor(self,editor):
		"""
		Unbinds the editor from the window.  This should only be called
		by the editor, and only when the editor is closing!
		"""
		pass
	def bindEditor(self,editor):
		"""
		Binds the given editor to this window.  This will be called when the editor
		is constructed, and causes a tab to appear for it.
		"""
		pass
	def getContainer(self):
		"""
		Returns the editor parent.
		"""
		pass
	container = property(getContainer)
	
	def bindPanel(self,editor,panel,onRight=True):
		"""
		Adds a tool panel to a side of the parent's content area.
		The panel can be on either the left or the right, and appears
		beneath the other panels.
		"""
		
	def closePanel(self,panel):
		"""
		Removes the tool panel from the parent's content area.
		By default, tool panels are hidden when editors are switched.
		"""

class Editor(wx.Panel):
	"""
	An editor class.  This class should be overridden, and you should add a method
	to the editors[] list for creating this Editor from an un-pickled data object.
	"""
	
	def __init__(self,parent,style=0, bind_later = False):
		"Basic editor initialization.  Make sure your editor calls this!"
		super(Editor, self).__init__(parent.get_container(),
		                             style=style | wx.EXPAND)
		self._title = 'Untitled'
		def f(title): pass
		self.display_title = f
		self.parent = parent
		self._modified = False
		if not bind_later: parent.bind_editor(self)

	@property
	def title(self):
		"Returns a title to show in the editor's tab."
		return self._title
		
	@title.setter
	def title(self, t):
		"Sets the title and updates it."
		self._title = t
		self.display_title(t)

	@property
	def modified(self):
		return self._modified

	@modified.setter
	def modified(self, t):
		if self._modified == t: return
		self._modified = t
		# TODO: the following is hacky:
		self.display_title(self._title + ('*' if self._modified else ''))

	def save(self, save_as = False):
		"Saves all changes to the edited item."
		pass
		
	def close(self,from_close_button=False):
		"""
		Called to close the editor.
		If this event occurs because of the close button,
		pass from_close_button=True on to closeEditor().  This
		will tell the editor parent not to manually remove
		the tab.
		"""
		
		# TODO: ask to save changes.
		if self.modified:
			dialog = wx.MessageDialog(self,
			                          "Do you want to save %s?" % self.title,
			                          "Save %s?" % self.title,
			                          wx.ICON_EXCLAMATION | wx.YES_NO)
			try:
				if dialog.ShowModal() == wx.ID_YES:
					self.save()
			finally:
				dialog.Destroy()
		self.parent.close_editor(self, from_close_button)

	def focus(self):
		"Called when the editor is shown."
		pass

	def blur(self):
		"Called when the editor is switched out."
		pass
